package org.mango.lineartable;


/**
 * 线性表的练习
 */

public class LinearList {

    LinearList nextNode = null;
    String data = null;

    public static void insertLastNode(LinearList head, String data){
         if(head == null){
             head = new LinearList();
             head.data = "";
         }
         LinearList tmp = head;
         LinearList node = new LinearList();
         node.data = data;
         while (tmp.nextNode != null){
             tmp = tmp.nextNode;
         }
         tmp.nextNode = node;
    }

    public static void insertNode(LinearList head, String data, String compareData){
         LinearList tmp = head;
         LinearList node = new LinearList();
         node.data = data;
         LinearList curNode = findNodeByData(head, compareData);
         node.nextNode = curNode.nextNode;
         curNode.nextNode = node;
    }

    private static LinearList findNodeByData(LinearList head, String compareData) {
        LinearList tmp = head;
        while (tmp != null){
            if (tmp.data.compareTo(compareData) == 0){
                return tmp;
            }
            tmp = tmp.nextNode;
        }
        return null;
    }

    public static void main(String[] args){
            LinearList node = new LinearList();
            node.data = "1";
            insertLastNode(node, "2");
            insertLastNode(node, "3");
            insertLastNode(node, "4");
            insertLastNode(node, "5");
            insertLastNode(node, "6");
            insertLastNode(node, "7");
            insertLastNode(node, "8");
            insertNode(node, "99999999999","6");
    }

}
